package com.example.chapp.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class httputil {
    public static boolean isNetworkActive(Context mContext){
        //1、提供各种各样的服务（CONNECTIVTY_AS）:获取连接服务
        ConnectivityManager systemService = (ConnectivityManager)mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        //2、拿到当前连接的网络（23/）
        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.M){
            //3.1、判断该连接网络是否可用
            Network activeNetwork = systemService.getActiveNetwork();
            if(activeNetwork==null){
                return false;
            }else{
                return true;
            }
        }else{
            //3.2、判断该连接网络是否可用
            NetworkInfo activeNetworkInfo = systemService.getActiveNetworkInfo();
            if(activeNetworkInfo!=null&&activeNetworkInfo.isConnected()){
                return activeNetworkInfo.isAvailable();
            }else{
                return false;
            }
        }
    }

    public static String decodeAsString(InputStream in){
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        StringBuilder builder = new StringBuilder();
        try {
            while ((line=reader.readLine())!=null){
                builder.append(line);
            }
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return builder.toString();
    }

    public static void sendRequest(String urlStr,
                                   String requestMethod,
                                   String param,
                                   HttpCallback callback){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(urlStr);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod(requestMethod);
                    conn.setConnectTimeout(16000);
                    conn.setReadTimeout(16000);
                    //conn.connect();
                    if(requestMethod.equals("POST")){
                        conn.setUseCaches(false);
                        conn.setDoInput(true);
                        conn.setDoOutput(true);
                        OutputStream output = conn.getOutputStream();
                        output.write(param.getBytes());
                        output.flush();
                    }

                    conn.connect();
                    if(conn.getResponseCode()==200){
                        InputStream input = conn.getInputStream();
                        callback.onSuccess(input);
                    }else{
                        callback.onFailure(conn.getResponseCode(),"访问网络失败");
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

}
